﻿/**
 * 一个 Action 代表一个功能，这些功能会显示为按钮或菜单。
 * 
 * @abstract
 */
Ext.define('Happy.Action', {
    extend: 'Ext.Action',
    alternateClassName: ['Happy.Action'],

    statics: {
        /**
         * 创建 Action。
         * 
         * 代码示例：
         *
         *     @example
         *     me.buttons = [Happy.createAction('save')];
         *     me.buttons = [Happy.createAction({ type: 'save' })];
         * 
         * @param {String|Object|Happy.Action}
         * @return {Happy.Action}
         */
        createAction: function (action) {
            if (action instanceof Ext.Action) {
                return action;
            }

            if (Ext.isString(action)) {
                action = { type: action };
            }

            return Ext.createByAlias('action.' + action.type, action);
        },

        /**
         * 创建 Action 数组。
         * 
         * 代码示例：
         *
         *     @example
         *     me.buttons = Happy.createActions([
         *         'save',
         *         'close'
         *     ]);
         *     me.buttons = Happy.createActions([
         *         { type: 'save' },
         *         'close'
         *     ]);
         * 
         * @param {Array} actions
         * @return {Array}
         */
        createActions: function (actions) {
            var me = this;

            return Ext.Array.map(actions, me.createAction);
        }
    },

    /**
     * @cfg 默认配置。
     */
    defaultConfig: {},

    constructor: function (config) {
        var me = this;

        config = config || {};

        Ext.applyIf(config, me.getDefaultConfig());

        me.callParent([config]);
    },

    /**
     * 返回默认配置。
     * 
     * @protect
     * @return {Object}
     */
    getDefaultConfig: function () {
        var me = this;

        return Ext.apply({
            handler: Ext.Function.bind(me.onClickHandler, me)
        }, me.defaultConfig || {});
    },

    /**
     * 点击事件的处理方法。
     * 
     * @abstract
     * @protected
     * @param {Ext.button.Button} button
     */
    onClickHandler: Ext.emptyFn,

    /**
     * 如果 selector 是一个 function，就直接返回 selector(button)；否则返回 button.up(selector)。
     * 
     * @protected
     * @param {Ext.button.Button} button
     * @param {string|Function} selector
     * @return {Ext.Component}
     */
    up: function (button, selector) {
        if (Ext.isFunction(selector)) {
            return selector(button);
        }

        return button.up(selector);
    }
}, function (Class) {
    Ext.apply(Happy, {
        /**
         * @member Happy
         * @method
         * @inheritdoc Happy.Action.#createAction
         */
        createAction: Ext.Function.alias(Class, 'createAction'),
        /**
         * @member Happy
         * @method
         * @inheritdoc Happy.Action.#createActions
         */
        createActions: Ext.Function.alias(Class, 'createActions')
    });
});